<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of addressbooks_model
 *
 * @author win7
 */
class addressbooks extends admin_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('admin/addressbooks_model', 'addressbooks');
    }

    public function index($offset = false) {
        $data['count'] = $this->addressbooks->filter(TRUE);
        $limit = metohd_get_uri('dblimit', ADMIN_LIMIT);
        $data['limit'] = $limit;
        $data['result'] = $this->addressbooks->filter(FALSE, $offset);
        $data['pagination'] = $this->admin_pagination('index', $this->addressbooks->filter(TRUE), $limit);
        $this->themes->render('addressbooks/index', $data);
    }

    public function view($id = false) {
        if (!$id) {
            redirect();
        }
        $falid_id = $this->addressbooks->is_found(array('addressbook.id' => $id));
        $data['result'] = $falid_id;

        $this->themes->render('addressbooks/view', $data);
    }

    public function edit($id = false) {
        if ($id) {
            $this->pass['addressbook'] = $id_id = $this->addressbooks->is_found(array('addressbook.id' => $id));
            if ($this->input->post()) {
                $this->session->set_userdata('post', $this->input->post());
                $this->form_validation->set_rules('name', lang('label_name'), 'required|trim');
                $this->form_validation->set_rules('mobile', lang('label_mobile'), 'required|trim');
                $this->form_validation->set_rules('city', lang('label_city'), 'required|trim');
                $this->form_validation->set_rules('address', lang('label_address'), 'required|trim|min_length[6]');
                if ($this->form_validation->run()) {
                    $city = $this->input->post('city');
                    $mobile = $this->input->post('mobile');
                    $name = $this->input->post('name');
                    if ($this->input->post('default')) {
                        $default = 'YES';
                    } else {
                        $default = 'NO';
                    }
                    $user_data['address'] = $this->input->post('address');
                    $user_data['default'] = $default;
                    $user_data['mobile'] = $mobile;
                    $user_data['cities_id'] = $city;
                    $user_data['name'] = $name;
                    if ($this->addressbooks->save($user_data, array('id' => $id), TRUE)) {
                        $this->themes->set_message(array('url' => 'addressbooks', 'type' => 'success', 'message' => 'message_edit_success'));
                    } else {
                        get_save_error();
                    }
                } else {
                    get_validation_errors();
                }
            }

            $this->load->model('admin/cities_model', 'cities');
            $this->pass['cities'] = $this->cities->get();
            $this->themes->render('addressbooks/edit', $this->pass);
        } else {
            show_404();
        }
    }

    public function all() {
        $id = $this->input->post('id');
        $doaction = $this->input->post('doaction');
        $this->empty_id_actionall($id);
        $this->empty_id_actionall($doaction, 'message_select_action_from_dropdown');

        switch ($doaction) {
            case 'default': {
                    $data['default'] = 'YES';
                    $this->addressbooks->save($data, array('id' => $id), TRUE);
                    $this->themes->set_message(array('url' => 'addressbooks', 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;
            case 'undefault': {
                    $data['default'] = 'NO';
                    $this->addressbooks->save($data, array('id' => $id), TRUE);
                    $this->themes->set_message(array('url' => 'addressbooks', 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;
            case 'delete': {
                    $this->addressbooks->is_used_in_invoices($id);
                    $this->admin->delete(array('id' => $id));
                    $this->themes->set_message(array('url' => 'addressbooks', 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;
            case 'no_trash': {
                    $data['trash'] = 'NO';
                    $this->admin->save($data, array('id' => $id), TRUE);
                    $this->themes->set_message(array('url' => 'addressbooks/trash', 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;
            case 'delete_final': {
                    $this->admin->delete(array('id' => $id));
                    $this->themes->set_message(array('url' => 'addressbooks/trash', 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;

            default:
                show_404();
                break;
        }
        //  echo json_encode($_POST['manager']);
    }

    public function no_trash($id = false) {
        if ($id) {
            $falid_id = $this->addressbooks->is_found(array('addressbook.id' => $id, 'addressbook.trash' => 'YES'), array(), 'row', $this->agent->referrer());
            $data['trash'] = 'NO';
            if ($this->addressbooks->save($data, array('id' => $id, 'trash' => 'YES'), TRUE)) {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function delete_final($id = false) {
        if ($id) {
            $falid_id = $this->addressbooks->is_found(array('addressbook.id' => $id, 'addressbook.trash' => 'YES'), array(), 'row', $this->agent->referrer());
            if ($this->addressbooks->delete(array('id' => $id), FALSE)) {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function delete($id = false) {
        if ($id) {
            $ref = $this->agent->referrer();
            $falid_id = $this->addressbooks->is_found(array('addressbook.id' => $id), array(), 'row', $ref);
            if ($falid_id->default == 'YES'){
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_can_not_delete_is_default'));
            }
            //$this->addressbooks->is_used_in_invoices($id);
            if ($this->addressbooks->delete(array('id' => $id))) {
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }
    
    public function trash($offset = false) {
        $limit = metohd_get_uri('dblimit', ADMIN_LIMIT);
        $count = $this->addressbooks->filter(TRUE, FALSE, TRUE);
        $this->pass['count'] = $count;
        $this->pass['limit'] = $limit;
        $this->pass['result'] = $this->addressbooks->filter(FALSE, $offset, TRUE);
        $this->pass['pagination'] = $this->admin_pagination('index', $count, $limit);
        $this->themes->render('addressbooks/trash', $this->pass);
    }

}

?>
